'use strict';

const Service = require('egg').Service;

class RoleService extends Service {
  // 新增和更新
  async save(params) {
    const User = this.ctx.state.user;
    params.updateBy = User.name;
    if (params._id) {
      return this.ctx.model.Role.findOneAndUpdate({ _id: params._id }, params, { upsert: true, new: true });
    }
    params.createBy = User.name;
    const saveDate = new this.ctx.model.Role(params);
    return saveDate.save();

  }

  // 查询所有数据
  async find(params = {}, page = 1, limit = 10) {
    return this.ctx.model.Role.find(params)
      .sort({ _id: -1 })
      .limit(limit)
      .skip((page - 1) * limit);
  }

  // 根据id查询角色
  async findById(ids) {
    return this.ctx.model.Role.find({ _id: { $in: ids } });
  }
}

module.exports = RoleService;
